package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.CnProductBean;
import com.yanchangapp.model.bean.ProductBean;
import com.yanchangapp.model.request.CreateContractRequest;
import com.yanchangapp.model.response.CreateContractResponse;
import com.yanchangapp.model.response.SupplierListResponse;
import com.yanchangapp.ui.view.ContractCreateAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-20.
 * description:合同新建P层
 */
public class ContractCreateAtPresenter extends BasePresenter<ContractCreateAtView> {

    public ContractCreateAtPresenter(BaseActivity context) {
        super(context);
    }

    public void loadSupplierData(){
        HeadApiRetrofit.getInstance().querySupperLists()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SupplierListResponse>() {
                    @Override
                    public void onNext(SupplierListResponse baseResponse) {
                        if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                            if (getView() != null){
                                getView().querySupplierSuccess(baseResponse.getData().getList());
                            }
                        }else {
                            if (getView() != null){
                                getView().querySupplierFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().querySupplierFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    // 创建合同
    public void createContract(String contractTime,
                               String supplycoId,
                               double totalMoney,
                               List<CnProductBean> products,
                               String covenant){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        CreateContractRequest contractRequest = new CreateContractRequest(
            contractTime,supplycoId,totalMoney,products,covenant);
        HeadApiRetrofit.getInstance().createContract(contractRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<CreateContractResponse>() {
                    @Override
                    public void onNext(CreateContractResponse createContractResponse) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == createContractResponse.getCode()){
                                getView().createContractSuccess(createContractResponse.getData().getId());
                            }else {
                                getView().createContractFailure(createContractResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().createContractFailure(exception.getErrorInfo());
                        }
                    }
                });
    }
}
